import CryptoJS from 'crypto-js'
// import JSEncrypt from 'jsencrypt'
var r = require('jsrsasign');
// const {JSEncrypt} = require('encryptlong')
export default {
    /**
     * @param {*需要加密的字符串 注：对象转化为json字符串再加密} word
     * @param {*aes加密需要的key值，这个key值后端同学会告诉你} keyStr
     * 两个方法中的第一个参数word是待加密或者解密的字符串，第二个参数keyStr是aes加密需要用到的16位字符串的key。如果想对一个js对象加密，需要先把该对象转成json字符串。
     */
    encrypt(word, keyStr) { // 对称加密
	// debugger
        var key = CryptoJS.enc.Utf8.parse(keyStr)
        var srcs = JSON.stringify(word)
		// console.log(srcs);
        var encrypted = CryptoJS.AES.encrypt(srcs, key,{ mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) // 加密模式为ECB，补码方式为PKCS5Padding（也就是PKCS7）
        return encrypted.toString()
    },
    //非对称加密key
    encryptRsa(key) {
		// debugger
		let pk="-----BEGIN PUBLIC KEY-----\n" +
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6+8a0I0epM+lCK5RE2ODAAuz0nFy58nyzxiPIbIdGnFbeNXjKaUZyfj+8IzFdgr/s5Sw5SbOhHk17za2rVPmJJ+q0J4PrCIn2ibhhIPiRb1REX18zVb4u+udh2cytxDA4K+KWL5GlGLkHSA7wWqhZKrOLvaec2Jaet+SAfkTy3fLtf6kZV/+gHL1N8uW8dlQvYddLLlLf1lOsh1Xtq6EQtn1VV+wNhQSvgjl7Tw4HcnyrSjBDSNOQ4YJNjdkzpjF/4CpWCW97x5tq1bYGCYJbVl7l07ZmcdSXMLuxMkz/M9Uq4JOPyusToIjzJuekfRIJ8zK3KVa8bgxl0h0uMdzmwIDAQAB\n" +
            "-----END PUBLIC KEY-----";
        // 加密
        // 读取解析pem格式的秘钥, 生成秘钥实例 (RSAKey) 
        var pub = r.KEYUTIL.getKey(pk);
        var enc = r.KJUR.crypto.Cipher.encrypt(key,pub);
		
        return r.hextob64(enc) //得到加密后的数据
    },
	//非对称私钥加密key加密后的前10位
	encryptRsa2(key) {
		// debugger 
		let priK = "-----BEGIN PRIVATE KEY-----\n" +
		            "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCzlyHUyZzabwylsfPt4DppyIUqcISnoAFlzOBSV2Pk+/VxJPote8GQ4kKs/nXYf3tAlVfP4VJpLNinOXBfANMjja6pDzMlk8NQr8BhtUfQYY44S5RF2/b45QT9LzoqsNTNah2hjtaTNrf/+1itjueezjJqE98Z675LWf50m7MdKsvzqUXuzp0lW1t79qBJt8RNXhstI2MSHma5GbdaS+fP1VC6fWwgbTFe+nJ53emRXsocsrGhdEqJ4S3dLVXYxUo+8s6BW3/pM3E4gS1xu8/4pV3ntmTRCYmZMnTODEbeqNrduHs1h6dYz9GYUWzoeZ/odgUviSRW7Y9S0K7WlHi5AgMBAAECggEAZreOVhJqcFULusmpZujVACP8ZmWsueM+aWdg9jednx6tJ2Wq4dDiyWdgE2oHJSjk6e+BJhkN9bH8yMiFiREDslYbC/7xlhCJqeCb/EafKIRjv7cau0owW/NBgz472qcBT+vdvsXf3JtzainscnhF6gKtuxZaiIzZbmeuritPquIVC76jYQQxK2vyplrstpJevo/Q7XmHmccw613djkJg3tymLOvYo1ZbQ7aQDNF3ZX1giI4HwgwjQYTSDaSiQ6YGQe+ZjaJnxvAju2Sm4wHdQJqZ4XzecOzv9IiwOictn/hO+HHRAfDH9u5dVNJAQutsUkgu6Yu8EkL9mW/JKWeCAQKBgQDsW2IIjTXakuu3DXYVD5gvrYIVnC1YuiDuzH/CWXPamb+LikL/sYjWiwnWrLG8pIETuZUkPYGPyfELTyPoUp7kWOQA6LbrjqVmM0RRGB4m47O+47JIPEQo7cpe2K+cr9NxTwJPaSFsg6XD8zfKtZjIouWtPcBlZKwrB03orXKxMQKBgQDChAKfQ+CXoVgiaHQ6yqu97GNOOWObWqXzDsjwMcWCJwyxiSmxsNFnyjq5jiqI8MRt3wOLCl9Bm+eWVyLOo4PlSMYH1JtdFF/pVxSv7svMoic65LfB5j0NnJ97bTYlv+F3ig/m4D9ewhuEpiscI8X9/1owwhh6HimSNtJHqzCeCQKBgQDdlqcIIWt16fw3d9VW1XH2cRLeM7eH0aiCeRLmIHsvXKlpp0w88DK8AUpOC0Xu98NTvM+IVGmdZtQnqCCf9KWmq3fHKmYj3W5PrIzGAnhyOVQRCOPiOS4luRnbZgbuPSFcv5gI3yVI2PuWA/Ywg6QjpiTyEEzKQmHvTT/LfqFiYQKBgQDAyQm4RBQC9JxfbdtUsa5JzFIYI3T/cnZrUcoBlbLFbmJ8Sw+R10eUcLCHW8n4c3ib1J60XZFPe2+qZ65N025T/y6WcDeTpy/wGqicPjarGEN9cVZWzRTRutut/u3duDGEwsdD8hmclxGjBPZRK3t+ZGlFTfnofqCBE+iRs6+50QKBgHUk401mNl2yVZYuA1FCN/a4aiU2mQpmcrdAd4qZrbdd9+e9GyeW3kWCWujE3Vvl7hNQ15b/oXEzMMnw/RV5UyldaNWbXIE9B1ESXWfcybrjsTjzmmRfPUTjlxsHD9vsvhutkgwCMXdO6kkUcOwTudXLDbpd/3lBuypsEjloZgeu\n" +
		            "-----END PRIVATE KEY-----";
		// 创建 Signature 对象
		    let signature=new r.KJUR.crypto.Signature({alg:"SHA1withRSA",prvkeypem:priK});    //!这里指定 私钥 pem!
		    signature.updateString(key);
		    let a = signature.sign();
		    let sign = r.hextob64(a);
	    return sign //得到加密后的数据
	},
    // 对称解密
    decrypt(word, keyStr) {
		// debugger;
        var key = CryptoJS.enc.Utf8.parse(keyStr);
		 // let decData = CryptoJS.enc.Base64.parse(word).toString(CryptoJS.enc.Utf8);
        var decrypt = CryptoJS.AES.decrypt(word, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return decrypt.toString(CryptoJS.enc.Utf8);
    },
    //非对称解密方法加密的key
    RSAdecrypt(enc) {
		// debugger;
		let priK = "-----BEGIN PRIVATE KEY-----\n" +
		            "MIIEvgIBADANBgkqhk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IWt16fw3d9VW1XH2cRLeM7eH0aiCeRLmIHsvXKlpp0w88DK8AUpOC0Xu98NTvM+IVGmdZtQnqCCf9KWmq3fHKmYj3W5PrIzGAnhyOVQRCOPiOS4luRnbZgbuPSFcv5gI3yVI2PuWA/Ywg6QjpiTyEEzKQmHvTT/LfqFiYQKBgQDAyQm4RBQC9JxfbdtUsa5JzFIYI3T/cnZrUcoBlbLFbmJ8Sw+R10eUcLCHW8n4c3ib1J60XZFPe2+qZ65N025T/y6WcDeTpy/wGqicPjarGEN9cVZWzRTRutut/u3duDGEwsdD8hmclxGjBPZRK3t+ZGlFTfnofqCBE+iRs6+50QKBgHUk401mNl2yVZYuA1FCN/a4aiU2mQpmcrdAd4qZrbdd9+e9GyeW3kWCWujE3Vvl7hNQ15b/oXEzMMnw/RV5UyldaNWbXIE9B1ESXWfcybrjsTjzmmRfPUTjlxsHD9vsvhutkgwCMXdO6kkUcOwTudXLDbpd/3lBuypsEjloZgeu\n" +
		            "-----END PRIVATE KEY-----";
       	    // 解密
       	        let prv = r.KEYUTIL.getKey(priK);
       	        let dec = r.KJUR.crypto.Cipher.decrypt(r.b64tohex(enc),prv);
        return dec;
    },
	//非对称解密方法解密key的前10位加密 延簽
	RSAdecrypt2(pas,sign) {
		// debugger;
		let pk="-----BEGIN PUBLIC KEY-----\n" +
		    "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6+8a0I0epM+lCK5RE2ODAAuz0nFy58nyzxiPIbIdGnFbeNXjKaUZyfj+8IzFdgr/s5Sw5SbOhHk17za2rVPmJJ+q0J4PrCIn2ibhhIPiRb1REX18zVb4u+udh2cytxDA4K+KWL5GlGLkHSA7wWqhZKrOLvaec2Jaet+SAfkTy3fLtf6kZV/+gHL1N8uW8dlQvYddLLlLf1lOsh1Xtq6EQtn1VV+wNhQSvgjl7Tw4HcnyrSjBDSNOQ4YJNjdkzpjF/4CpWCW97x5tq1bYGCYJbVl7l07ZmcdSXMLuxMkz/M9Uq4JOPyusToIjzJuekfRIJ8zK3KVa8bgxl0h0uMdzmwIDAQAB\n" +
		    "-----END PUBLIC KEY-----";
		    // 验签
		    // !要重新new 一个Signature, 否则, 取摘要和签名时取得摘要不一样, 导致验签误报失败(原因不明)!
		    let signatureVf = new r.KJUR.crypto.Signature({alg:"SHA1withRSA",prvkeypem:pk});
		    signatureVf.updateString(pas);
		    // !接受的参数是16进制字符串!
		    let b = signatureVf.verify(r.b64tohex(sign));
		    // console.log("jsrsasign verify: "+b);
		return b;
	},
    //生成随机数
    RandomNumber(pasLen) {
        var pasArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
        var password = '';
        var pasArrLen = pasArr.length;
        for (var i = 0; i < pasLen; i++) {
            var x = Math.floor(Math.random() * pasArrLen);
            password += pasArr[x];
        }
        return password;
    },
    //访问接口前对数据加密的公用方法
    /*userParam  字符串拼接*/
    PublicEntryption(userParam) {
        //token token值
        //Page 当前页
        //PageSize 每页默认10条
        //UserType type为1时 参会企业  需要传此参数  传1采购商 2供货商
        let ariginalKey = this.RandomNumber(16); //生成随机得key值
        let EntryptionUserParam = this.encrypt(userParam, ariginalKey); //用随机生成的key值对称机密用户信息
        let encryptRsakey = this.encryptRsa(ariginalKey); //对随机生成的key值进行非对称加密
		let encryptRsakey2 = this.encryptRsa2(encryptRsakey); //对随机生成的key值进行非对称加密
        let reslutModel = {
            encryptData: EntryptionUserParam,
            key: encryptRsakey,
			keyabstract:encryptRsakey2
        };
		// console.log(reslutModel);
		// debugger
        return JSON.stringify(reslutModel)//encodeURIComponent(JSON.stringify(reslutModel))
    },
    //解密
    PublicDecode(response) {
		// debugger;
		let jsonresponse = JSON.parse(response)
        let keyParam = jsonresponse.key; //对解密要用到得key值进行解码
        let dataParam = jsonresponse.encryptData; //对返回来的数据进行解码
		let keystrParam= jsonresponse.keyabstract;
		let keystr=this.RSAdecrypt2(keyParam,keystrParam);
		// debugger
		if(keystr){
			let key = this.RSAdecrypt(keyParam); //非对称解密 key值
			let data = this.decrypt(dataParam, key); //用得到的key值对返回来数据进行对称解密
			// debugger
			// console.log(data)
			return data
		}else{
			return false
		}
		
        
    }

}